﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using LayeredArchitectureSample.DataTransferObjects;
using LayeredArchitectureSample.DomainModel;
using LayeredArchitectureSample.Infrastructure;
using LayeredArchitectureSample.ServiceContracts;

namespace LayeredArchitectureSample.Services
{
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, InstanceContextMode = InstanceContextMode.Single)]
    [ExceptionMarshallingBehavior]
    public class RemoteWareRepository : IRemoteWareRepository
    {
        private readonly IWareRepository _wareRepository;

        public RemoteWareRepository(IWareRepository wareRepository)
        {
            if (wareRepository == null) throw new ArgumentNullException("wareRepository");

            _wareRepository = wareRepository;
        }

        public void RemoveWare(long id)
        {
            _wareRepository.RemoveWare(id);
        }

        public WareDto AddNewWare(string code)
        {
            var newWare = _wareRepository.AddNewWare(code);

            return new WareDto(newWare);
        }

        public IEnumerable<WareDto> GetAll()
        {
            return new List<WareDto>(_wareRepository.GetAll().Select(x => new WareDto(x)));
        }
    }
}